<template>
  <div>
    <h2>待办事项</h2>
    
    <el-table :data="tasks" style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="workflow_type" label="流程类型" width="120" />
      <el-table-column prop="task_name" label="任务名称" width="150" />
      <el-table-column prop="status" label="状态" width="120">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" />
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button
            size="small"
            type="primary"
            @click="handleTask(scope.row)"
            :disabled="scope.row.status !== 'pending'"
          >
            处理
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      v-model="taskDialogVisible"
      :title="currentTask ? `处理任务 - ${currentTask.task_name}` : ''"
      width="500px"
    >
      <el-form :model="taskForm" label-width="100px">
        <el-form-item label="处理意见">
          <el-input
            v-model="taskForm.comment"
            type="textarea"
            :rows="3"
            placeholder="请输入处理意见"
          />
        </el-form-item>
        <el-form-item label="操作">
          <el-radio-group v-model="taskForm.action">
            <el-radio label="approve">通过</el-radio>
            <el-radio label="reject">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="taskDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitTask">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const tasks = ref([])
const taskDialogVisible = ref(false)
const currentTask = ref(null)
const taskForm = ref({
  comment: '',
  action: 'approve'
})

const getStatusType = (status) => {
  const types = {
    'pending': 'warning',
    'completed': 'success',
    'rejected': 'danger'
  }
  return types[status] || 'info'
}

const fetchTasks = async () => {
  try {
    const response = await axios.get('/workflow/tasks')
    tasks.value = response.data
  } catch (error) {
    ElMessage.error('获取待办事项失败')
  }
}

const handleTask = (task) => {
  currentTask.value = task
  taskForm.value = {
    comment: '',
    action: 'approve'
  }
  taskDialogVisible.value = true
}

const submitTask = async () => {
  if (!currentTask.value) return
  
  try {
    const endpoint = taskForm.value.action === 'approve' ? 'approve' : 'reject'
    await axios.post(`/workflow/${currentTask.value.workflow_id}/${endpoint}`, {
      comment: taskForm.value.comment
    })
    
    ElMessage.success('任务处理成功')
    taskDialogVisible.value = false
    fetchTasks()
  } catch (error) {
    ElMessage.error('任务处理失败')
  }
}

onMounted(() => {
  fetchTasks()
})
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 